<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>

  <script>
    // var button = document.querySelectorAll("button") as HTMLDivElement
    var button = document.querySelectorAll("button")

    for (let i = 0; i < button.length; i++) {
      
      var btnEl = button[i]
      btnEl.index = i
      
      btnEl.onclick = function () {  
        console.log(this.index);
      }
    }
    
  </script>
</body>
</html>